﻿using System;

using System.Globalization;
using System.Collections.ObjectModel;
using System.Web.UI;
using System.Web.UI.WebControls;

using Business;
using Common.Constants;
using Entities;
namespace ServicesDirectory.UserControl
{
    public partial class ListContact1 : System.Web.UI.UserControl
    {
        /// <summary>
        /// Occurs when [close control].
        /// </summary>
        public event EventHandler CloseControl;
        /// <summary>
        /// Occurs when [get contact id].
        /// </summary>
        public event EventHandler GetContactId;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            cmdSearchContact.Click += CmdSearchContact_Click;
            rptContacts.ItemDataBound += RptContacts_ItemDataBound;
            chkInactive.CheckedChanged += Inactive_CheckedChanged;
            cmdEdit.Click += CmdEdit_Click;
            cmdCreate.Click += CmdCreate_Click;
            cmdNone.Click += None_Click;
            cmdSelect.Click += Select_Click;
            ManagerContact1.CloseControl += ManagerContact1_CloseControl;
            cmdClear.Click += CmdClear_Click;
            const string draggable = "$( '#draggableManagerContact' ).draggable();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "UpdatePannelManagerContact", draggable, true);
            if (listEdit.Visible)
            {
                const string visiblefunction = "$( '#overlay2' ).show();";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ListContactKeyContactEnable1", visiblefunction, true);
            }
            if (!IsPostBack)
            {
                string message;
                var lstContact = ContactBusiness.Instance.ListContact(string.Empty, string.Empty, !chkInactive.Checked, out message);
                if (lstContact != null)
                {
                    lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture), 15, lstContact);
                    rptContacts.DataBind();
                }
                else
                {
                    lblAnnounce.Text = message;
                    lblAnnounce.Visible = true;
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the CmdClear control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void CmdClear_Click(object sender, EventArgs e)
        {
            txtFirstName.Text = "";
            txtSurname.Text = "";
            string message;
            var lstContact = ContactBusiness.Instance.ListContact(string.Empty, string.Empty, !chkInactive.Checked, out message);
            if (lstContact != null && lstContact.Count > 0)
            {
                pnNoRecord.Visible = false;
                lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture), 15, lstContact);
                rptContacts.DataBind();
            }
            else
            {
                rptContacts.DataSource = null;
                rptContacts.DataBind();
                txtPageNumber.Value = "0";
                lblTotalPage.Text = "0";
                pnNoRecord.Visible = true;
                lblAnnounce.Text = message;
                lblAnnounce.Visible = true;
            }
        }

        /// <summary>
        /// Handles the CloseControl event of the ManagerContact1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ManagerContact1_CloseControl(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(sender.ToString()) && int.Parse(sender.ToString(), CultureInfo.InvariantCulture) == (int)ModeForm.Update)
                SearchContact();
            if (!string.IsNullOrEmpty(sender.ToString()) && int.Parse(sender.ToString(), CultureInfo.InvariantCulture) == (int)ModeForm.Add)
                GetContact(ManagerContact1.ContactId);

            ManagerContact1.SetModeForm("0");
            listEdit.Visible = false;
            const string visiblefunction = "$( '#overlay2' ).hide();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ListContactKeyContactEnable4", visiblefunction, true);

        }


        /// <summary>
        /// Handles the Click event of the Select control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void Select_Click(object sender, EventArgs e)
        {
            var selectContact = false;
            foreach (RepeaterItem item in rptContacts.Items)
            {

                var rdo = (RadioButton)item.FindControl("rbtSelect");
                var contactId = item.FindControl("hdContactId") as HiddenField;
                if (rdo.Checked)
                {

                    if (contactId != null)
                    {
                        string message;
                        var oContact = ContactBusiness.Instance.GetContact(contactId.Value, out  message);
                        if (oContact != null)
                            if (GetContactId != null)
                                GetContactId(oContact, e);

                        selectContact = true;
                        break;
                    }
                }
            }
            if (selectContact)
            {

                if (CloseControl != null)
                {
                    txtFirstName.Text = "";
                    txtSurname.Text = "";
                    string message;
                    var lstContact = ContactBusiness.Instance.ListContact(string.Empty, string.Empty, !chkInactive.Checked, out message);
                    if (lstContact != null && lstContact.Count > 0)
                    {
                        pnNoRecord.Visible = false;
                        lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                        rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture), 15, lstContact);
                        rptContacts.DataBind();
                    }
                    else
                    {
                        rptContacts.DataSource = null;
                        rptContacts.DataBind();
                        txtPageNumber.Value = "0";
                        lblTotalPage.Text = "0";
                        pnNoRecord.Visible = true;
                    }
                    CloseControl(sender, e);
                }
            }
            else
            {
                lblAnnounce.Text = " you have to choose a row ";
                lblAnnounce.Visible = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the None control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void None_Click(object sender, EventArgs e)
        {
            if (GetContactId != null)
                GetContactId(null, e);
            if (CloseControl != null)
            {
                txtFirstName.Text = "";
                txtSurname.Text = "";
                string message;
                var lstContact = ContactBusiness.Instance.ListContact(string.Empty, string.Empty, !chkInactive.Checked, out message);
                if (lstContact != null && lstContact.Count > 0)
                {
                    pnNoRecord.Visible = false;
                    lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture), 15, lstContact);
                    rptContacts.DataBind();
                }
                else
                {
                    rptContacts.DataSource = null;
                    rptContacts.DataBind();
                    txtPageNumber.Value = "0";
                    lblTotalPage.Text = "0";
                    pnNoRecord.Visible = true;
                }
                CloseControl(sender, e);
            }

        }


        /// <summary>
        /// Handles the Click event of the cmdCreate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void CmdCreate_Click(object sender, EventArgs e)
        {
            ManagerContact1.SetModeForm(((int)ModeForm.Add).ToString(CultureInfo.InvariantCulture));
            const string visiblefunction = "$( '#overlay2' ).show();";
            ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ListContactKeyContactEnable2", visiblefunction, true);
            listEdit.Visible = true;
        }



        /// <summary>
        /// Handles the Click event of the cmdEdit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void CmdEdit_Click(object sender, EventArgs e)
        {

            var selectContact = false;
            foreach (RepeaterItem item in rptContacts.Items)
            {

                var rdo = (RadioButton)item.FindControl("rbtSelect");
                var contactId = item.FindControl("hdContactId") as HiddenField;
                if (rdo.Checked)
                {

                    if (contactId != null)
                    {
                        ManagerContact1.SetModeForm(((int)ModeForm.Update).ToString(CultureInfo.InvariantCulture));
                        ManagerContact1.ContactId = contactId.Value;
                        ManagerContact1.BindContact();
                        selectContact = true;

                    }

                    break;
                }
            }
            if (selectContact)
            {
                const string visiblefunction = "$( '#overlay2' ).show();";
                ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), "ListContactKeyContactEnable3", visiblefunction, true);
                listEdit.Visible = true;

            }
            else
            {
                lblAnnounce.Text = " you have to choose a row ";
                lblAnnounce.Visible = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdSearchContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void CmdSearchContact_Click(object sender, EventArgs e)
        {
            txtPageNumber.Value = "1";
            SearchContact();

        }

        /// <summary>
        /// Handles the ItemDataBound event of the rptContacts control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void RptContacts_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
                return;
            var lblIsactive = e.Item.FindControl("lblIsactive") as Label;
            if (lblIsactive != null)
            {
                if (lblIsactive.Text == "True" || lblIsactive.Text == "Yes")
                    lblIsactive.Text = "Yes";
                else
                {
                    lblIsactive.Text = "No";
                }
            }
            var rdo = (RadioButton)e.Item.FindControl("rbtSelect");
            const string script = "SetUniqueRadioButton(this)";
            rdo.Attributes.Add("onclick", script);


        }

        /// <summary>
        /// Handles the CheckedChanged event of the chkInActive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Inactive_CheckedChanged(object sender, EventArgs e)
        {
            txtPageNumber.Value = "1";
            SearchContact();
        }
        /// <summary>
        /// Searches the contact.
        /// </summary>
        void SearchContact()
        {

            string message;
            var lstContact = ContactBusiness.Instance.ListContact(txtFirstName.Text, txtSurname.Text, !chkInactive.Checked, out message);
            if (lstContact != null && lstContact.Count > 0)
            {
                pnNoRecord.Visible = false;
                lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);

                rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture), 15, lstContact);
                rptContacts.DataBind();
            }
            else
            {
                rptContacts.DataSource = null;
                rptContacts.DataBind();
                txtPageNumber.Value = "0";
                lblTotalPage.Text = "0";
                pnNoRecord.Visible = true;

            }
        }
        /// <summary>
        /// Gets the contact.
        /// </summary>
        /// <param name="contactId">The contact id.</param>
        void GetContact(string contactId)
        {
            string message;
            var contact = ContactBusiness.Instance.GetContact(contactId, out message);
            var lstContact = new Collection<Contact> { contact };
            if (lstContact.Count > 0)
            {
                lblTotalPage.Text = "1";
                rptContacts.DataSource = lstContact;
                rptContacts.DataBind();
            }
            else
            {
                lblAnnounce.Text = message;
                lblAnnounce.Visible = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdClose control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdClose_Click(object sender, EventArgs e)
        {

            if (CloseControl != null)
            {
                txtFirstName.Text = "";
                txtSurname.Text = "";
                txtPageNumber.Value = "1";
                string message;
                var lstContact = ContactBusiness.Instance.ListContact(string.Empty, string.Empty, !chkInactive.Checked, out message);
                if (lstContact != null && lstContact.Count > 0)
                {
                    pnNoRecord.Visible = false;
                    lblTotalPage.Text = lstContact.Count % 15 == 0 ? (lstContact.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstContact.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptContacts.DataSource = ContactBusiness.Instance.PagingContact(int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture), 15, lstContact);
                    rptContacts.DataBind();
                }
                else
                {
                    rptContacts.DataSource = null;
                    rptContacts.DataBind();
                    txtPageNumber.Value = "0";
                    lblTotalPage.Text = "0";
                    pnNoRecord.Visible = true;
                }
                CloseControl(sender, e);
            }

        }

        /// <summary>
        /// Handles the Click event of the cmdBackAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdBackAll_Click(object sender, EventArgs e)
        {
            txtPageNumber.Value = "1";
            SearchContact();
        }

        /// <summary>
        /// Handles the Click event of the cmdBackList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Backlist_Click(object sender, EventArgs e)
        {
            if (int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture) - 1 > 0)
            {
                txtPageNumber.Value = (int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture) - 1).ToString(CultureInfo.InvariantCulture);
                SearchContact();
            }
        }

        /// <summary>
        /// Handles the Click event of the cmdNextAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdNextAll_Click(object sender, EventArgs e)
        {
            txtPageNumber.Value = lblTotalPage.Text;
            SearchContact();
        }

        /// <summary>
        /// Handles the Click event of the cmdNext control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdNext_Click(object sender, EventArgs e)
        {
            if ((int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture) + 1) <= ((int.Parse(lblTotalPage.Text, CultureInfo.InvariantCulture))))
            {
                txtPageNumber.Value = (int.Parse(txtPageNumber.Value, CultureInfo.InvariantCulture) + 1).ToString(CultureInfo.InvariantCulture);
                SearchContact();
            }
        }



    }
}
